// Kony

#include "UI/Widget/GlobeBarWidget.h"
#include "UI/WidgetController/OverlayWidgetController.h"
#include "AbilitySystem/BaseAttributeSet.h"
#include "Components/ProgressBar.h"

//静态变量定义
bool UGlobeBarWidget::bGhostHealthBarFollowing = true;
bool UGlobeBarWidget::bGhostManaBarFollowing = true;

UGlobeBarWidget::UGlobeBarWidget()
{
	CurrentGhostHealth = 0.0f;
	CurrentGhostMana = 0.0f;
	TargetGhostHealth = 0.0f;
	TargetGhostMana = 0.0f;
	CurrentHealth = 0.0f;
	CurrentMana = 0.0f;
	MaxHealth = 100.0f;
	MaxMana = 100.0f;
}

void UGlobeBarWidget::NativeConstruct()
{
	Super::NativeConstruct();
	
	//绑定幽灵属性变化回调
	if (UOverlayWidgetController* OverlayController = Cast<UOverlayWidgetController>(WidgetController))
	{
		//绑定幽灵血球变化回调
		OverlayController->OnGhostHealthChanged.AddDynamic(this, &UGlobeBarWidget::OnGhostHealthChanged);
		//绑定幽灵法力球变化回调
		OverlayController->OnGhostManaChanged.AddDynamic(this, &UGlobeBarWidget::OnGhostManaChanged);
		
		//绑定当前生命值和法力值变化回调
		OverlayController->OnHealthChanged.AddDynamic(this, &UGlobeBarWidget::OnHealthChanged);
		OverlayController->OnManaChanged.AddDynamic(this, &UGlobeBarWidget::OnManaChanged);
		OverlayController->OnMaxHealthChanged.AddDynamic(this, &UGlobeBarWidget::OnMaxHealthChanged);
		OverlayController->OnMaxManaChanged.AddDynamic(this, &UGlobeBarWidget::OnMaxManaChanged);
		
		//初始化时获取当前值（这里暂时使用默认值，实际值会在回调中更新）
		CurrentGhostHealth = 100.0f;
		CurrentGhostMana = 100.0f;
		TargetGhostHealth = 100.0f;
		TargetGhostMana = 100.0f;
		CurrentHealth = 100.0f;
		CurrentMana = 100.0f;
		MaxHealth = 100.0f;
		MaxMana = 100.0f;
	}
}

void UGlobeBarWidget::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
	Super::NativeTick(MyGeometry, InDeltaTime);
	
	//更新幽灵血球和法力球显示
	UpdateGhostHealthBar();
	UpdateGhostManaBar();
}

void UGlobeBarWidget::OnGhostHealthChanged(float NewGhostHealth)
{
	TargetGhostHealth = NewGhostHealth;
	
	//如果不跟随，立即更新显示值
	if (!bGhostHealthBarFollowing)
	{
		CurrentGhostHealth = TargetGhostHealth;
	}
}

void UGlobeBarWidget::OnGhostManaChanged(float NewGhostMana)
{
	TargetGhostMana = NewGhostMana;
	
	//如果不跟随，立即更新显示值
	if (!bGhostManaBarFollowing)
	{
		CurrentGhostMana = TargetGhostMana;
	}
}

//当前属性变化回调函数实现
void UGlobeBarWidget::OnHealthChanged(float NewHealth)
{
	CurrentHealth = NewHealth;
}

void UGlobeBarWidget::OnManaChanged(float NewMana)
{
	CurrentMana = NewMana;
}

void UGlobeBarWidget::OnMaxHealthChanged(float NewMaxHealth)
{
	MaxHealth = NewMaxHealth;
}

void UGlobeBarWidget::OnMaxManaChanged(float NewMaxMana)
{
	MaxMana = NewMaxMana;
}

void UGlobeBarWidget::UpdateGhostHealthBar()
{
	if (MaxHealth <= 0.0f) return;
	
	//如果启用跟随，则缓慢移动到当前生命值
	if (bGhostHealthBarFollowing)
	{
		float TargetPercent = CurrentHealth / MaxHealth;
		float CurrentPercent = CurrentGhostHealth / MaxHealth;
		
		//计算移动距离
		float MoveDistance = FollowSpeed * 0.01f; // 转换为百分比
		
		if (FMath::Abs(TargetPercent - CurrentPercent) > MoveDistance)
		{
			//向目标值移动
			if (TargetPercent > CurrentPercent)
			{
				CurrentPercent += MoveDistance;
			}
			else
			{
				CurrentPercent -= MoveDistance;
			}
		}
		else
		{
			//接近目标值，直接设置为目标值
			CurrentPercent = TargetPercent;
		}
		
		CurrentGhostHealth = CurrentPercent * MaxHealth;
	}
	//如果不跟随，CurrentGhostHealth已经在OnGhostHealthChanged中更新了
}

void UGlobeBarWidget::UpdateGhostManaBar()
{
	if (MaxMana <= 0.0f) return;
	
	//如果启用跟随，则缓慢移动到当前法力值
	if (bGhostManaBarFollowing)
	{
		float TargetPercent = CurrentMana / MaxMana;
		float CurrentPercent = CurrentGhostMana / MaxMana;
		
		//计算移动距离
		float MoveDistance = FollowSpeed * 0.01f; // 转换为百分比
		
		if (FMath::Abs(TargetPercent - CurrentPercent) > MoveDistance)
		{
			//向目标值移动
			if (TargetPercent > CurrentPercent)
			{
				CurrentPercent += MoveDistance;
			}
			else
			{
				CurrentPercent -= MoveDistance;
			}
		}
		else
		{
			//接近目标值，直接设置为目标值
			CurrentPercent = TargetPercent;
		}
		
		CurrentGhostMana = CurrentPercent * MaxMana;
	}
	//如果不跟随，CurrentGhostMana已经在OnGhostManaChanged中更新了
}

//静态函数实现
void UGlobeBarWidget::SetGhostHealthBarFollowing(bool bEnableFollowing)
{
	bGhostHealthBarFollowing = bEnableFollowing;
}

bool UGlobeBarWidget::IsGhostHealthBarFollowing()
{
	return bGhostHealthBarFollowing;
}

void UGlobeBarWidget::SetGhostManaBarFollowing(bool bEnableFollowing)
{
	bGhostManaBarFollowing = bEnableFollowing;
}

bool UGlobeBarWidget::IsGhostManaBarFollowing()
{
	return bGhostManaBarFollowing;
}

//UI更新函数实现
float UGlobeBarWidget::GetCurrentGhostHealthPercent() const
{
	if (MaxHealth > 0.0f)
	{
		return CurrentGhostHealth / MaxHealth;
	}
	return 0.0f;
}

float UGlobeBarWidget::GetCurrentGhostManaPercent() const
{
	if (MaxMana > 0.0f)
	{
		return CurrentGhostMana / MaxMana;
	}
	return 0.0f;
}

float UGlobeBarWidget::GetCurrentGhostHealth() const
{
	return CurrentGhostHealth;
}

float UGlobeBarWidget::GetCurrentGhostMana() const
{
	return CurrentGhostMana;
} 